<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit;
}

require_once XOOPS_ROOT_PATH . '/core/XCube_ActionForm.class.php';
require_once XOOPS_MODULE_PATH . '/legacy/class/Legacy_Validator.class.php';

/**
 * Mileage_Admin_MileEditForm
**/
class Mileage_Admin_MileEditForm extends XCube_ActionForm
{
	private $_toUid = NULL;
	/**
	 * getTokenName
	 * 
	 * @param   void
	 * 
	 * @return  string
	**/
	public function getTokenName()
	{
		return "module.mileage.Admin_MileEditForm.TOKEN";
	}

	/**
	 * prepare
	 * 
	 * @param   void
	 * 
	 * @return  void
	**/
	public function prepare()
	{
		//
		// Set form properties
		//
		$this->mFormProperties['mile_id']      = new XCube_IntProperty('mile_id');
		$this->mFormProperties['name']         = new XCube_StringProperty('name');
		$this->mFormProperties['point']        = new XCube_IntProperty('point');
		$this->mFormProperties['uid']          = new XCube_IntProperty('uid');
		$this->mFormProperties['reg_unixtime'] = new XCube_IntProperty('reg_unixtime');
		$this->mFormProperties['url']          = new XCube_StringProperty('url');
		$this->mFormProperties['suburl']       = new XCube_IntProperty('suburl');
		$this->mFormProperties['interval']     = new XCube_IntProperty('interval');
		$this->mFormProperties['group']        = new XCube_StringProperty('group');

		//
		// Set field properties
		//
		$this->mFieldProperties['mile_id'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['mile_id']->setDependsByArray(array('required'));
		$this->mFieldProperties['mile_id']->addMessage('required', _MD_MILEAGE_ERROR_REQUIRED, _MD_MILEAGE_LANG_MILE_ID);

		$this->mFieldProperties['name'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['name']->setDependsByArray(array('required','maxlength'));
		$this->mFieldProperties['name']->addMessage('required', _MD_MILEAGE_ERROR_REQUIRED, _MD_MILEAGE_LANG_NAME);
		$this->mFieldProperties['name']->addMessage('maxlength', _MD_MILEAGE_ERROR_MAXLENGTH, _MD_MILEAGE_LANG_NAME, '255');
		$this->mFieldProperties['name']->addVar('maxlength', '255');

		$this->mFieldProperties['point'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['point']->setDependsByArray(array('required'));
		$this->mFieldProperties['point']->addMessage('required', _MD_MILEAGE_ERROR_REQUIRED, _MD_MILEAGE_LANG_POINT);

		$this->mFieldProperties['uid'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['uid']->setDependsByArray(array('required'));
		$this->mFieldProperties['uid']->addMessage('required', _MD_MILEAGE_ERROR_REQUIRED, _MD_MILEAGE_LANG_UID);

		$this->mFieldProperties['reg_unixtime'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['reg_unixtime']->setDependsByArray(array('required'));
		$this->mFieldProperties['reg_unixtime']->addMessage('required', _MD_MILEAGE_ERROR_REQUIRED, _MD_MILEAGE_LANG_REG_UNIXTIME);

		$this->mFieldProperties['url'] = new XCube_FieldProperty($this);
		$this->mFieldProperties['url']->setDependsByArray(array('required','maxlength'));
		$this->mFieldProperties['url']->addMessage('required', _MD_MILEAGE_ERROR_REQUIRED, _MD_MILEAGE_LANG_URL);
		$this->mFieldProperties['url']->addMessage('maxlength', _MD_MILEAGE_ERROR_MAXLENGTH, _MD_MILEAGE_LANG_URL, '255');
		$this->mFieldProperties['url']->addVar('maxlength', '255');

	}

	public function fetchName()
	{
		$root =& XCube_Root::getSingleton();
		$url = $root->mContext->mRequest->getRequest('url');
		if (strtolower($url) == 'charge') {
			$this->_toUid = $root->mContext->mRequest->getRequest('toUid');
			if (Mileage_Utils::getUnameByUid($this->_toUid)) {
				$point = $root->mContext->mRequest->getRequest('point');
				$name = 'Charged '. intval($point) . ' points to UID:' . intval($this->_toUid);
				$this->set('name', $name);
			}
		}
	}

	public function fetchSuburl()
	{
		$root =& XCube_Root::getSingleton();
		$suburl = $root->mContext->mRequest->getRequest('suburl');
		$suburl = empty($suburl) ? 0: 1;
		$this->set('suburl', $suburl);
	}

	public function fetchGroup()
	{
		$root =& XCube_Root::getSingleton();
		$groups = $root->mContext->mRequest->getRequest('group');
		if (is_array($groups) && count($groups) > 0) {
			$groups = array_map('intval', $groups);
			$group_text = '[' . implode('][', $groups) . ']';
			$this->set('group', $group_text);
		}
	}

	public function fetchInterval()
	{
		$root =& XCube_Root::getSingleton();
		$interval = $root->mContext->mRequest->getRequest('interval');
		$url = $root->mContext->mRequest->getRequest('url');
		if (strtolower($url) == 'charge') {
			$interval = 0;
		}
		if (($interval >= 0 && $interval <= 4) || $interval == 99) {
			$interval = intval($interval);
		} else {
			$interval = 0;
		}
		$this->set('interval', $interval);
	}

	public function fetchUrl()
	{
		$root =& XCube_Root::getSingleton();
		$url = $root->mContext->mRequest->getRequest('url');
		if (strtolower($url) == 'login') {
			$this->set('url', 'login');
		}
		if (strtolower($url) == 'register') {
			$this->set('url', 'register');
		}
		if (strpos(strtolower($url), 'ticket') === 0) {
			$url = strtolower(trim($url));
			if (preg_match('/^ticket.*\d$/', $url)) {
				$url = $url . '|';
			}
			$this->set('url', $url);
		}
	}

	public function fetchUid()
	{
		$root =& XCube_Root::getSingleton();
		$uid = 0;
		if (is_object($root->mContext->mXoopsUser)) {
			$uid = $root->mContext->mXoopsUser->get('uid');
		}
		$this->set('uid', $uid);
	}

	public function fetchReg_unixtime()
	{
		$this->set('reg_unixtime', time());
	}

	public function validateUrl()
	{
		$url = $this->get('url');
		if (!(
				strtolower($url) == 'login' || 
				strtolower($url) == 'register' || 
				(strtolower($url) == 'charge' && $this->_toUid !== NULL) ||
				strpos($url, XOOPS_URL) === 0 || 
				strpos(strtolower($url), 'ticket') === 0
			) 
			|| preg_match("/[\\x0-\\x1f]|[\\x7f]/", $url)
		) {
			$this->mErrorFlag = TRUE;
			$this->addErrorMessage(_MD_MILEAGE_ERROR_URL);
		}
	}


	/**
	 * load
	 * 
	 * @param   XoopsSimpleObject  &$obj
	 * 
	 * @return  void
	**/
	public function load(&$obj)
	{
		$this->set('mile_id',      $obj->get('mile_id'));
		$this->set('name',         $obj->get('name'));
		$this->set('point',        $obj->get('point'));
		$this->set('uid',          $obj->get('uid'));
		$this->set('reg_unixtime', $obj->get('reg_unixtime'));
		$this->set('url',          $obj->get('url'));
		$this->set('suburl',       $obj->get('suburl'));
		$this->set('interval',     $obj->get('interval'));
		$this->set('group',        $obj->get('group'));
	}

	/**
	 * update
	 * 
	 * @param   XoopsSimpleObject  &$obj
	 * 
	 * @return  void
	**/
	public function update(&$obj)
	{
		$obj->set('mile_id',      $this->get('mile_id'));
		$obj->set('name',         $this->get('name'));
		$obj->set('point',        $this->get('point'));
		$obj->set('uid',          $this->get('uid'));
		$obj->set('reg_unixtime', $this->get('reg_unixtime'));
		$obj->set('url',          $this->get('url'));
		$obj->set('suburl',       $this->get('suburl'));
		$obj->set('interval',     $this->get('interval'));
		$obj->set('group',        $this->get('group'));
	}

}
